package connect;

import beans.Customers;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author malguy-wang sir
 * @create ${year}-${month}-${day}-${time}
 */
public class druid {
    private static Connection druid() throws Exception{
        Properties pro = new Properties();
        //引入配置文件，druidTest.class.getClassLoader()根据当前类是哪个类来写
        pro.load(druid.class.getClassLoader().getResourceAsStream("druid.properties"));
        //获取数据源
        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
        //从数据源（连接池）里获取连接
        Connection conn = ds.getConnection();
        return conn;
    }
    private static void close(Connection conn, PreparedStatement ps){
        try {
            if(conn!=null) conn.close();
            if(ps!=null) ps.close();
        } catch (Exception e) {
        }
    }
    private static void insert(Object...args) throws Exception{
        QueryRunner qr = new QueryRunner();
        Connection conn = druid();
        String sql = "insert into customers(name,email,birth,photo) values(?,?,?,?)";
        int count = qr.update(conn,sql,args[0],args[1],args[2],null);
        System.out.println("添加了" + count + "条记录");
        close(conn, null);
    }
    //test方法不能是静态的
    public static void ins(Scanner scan){
        System.out.println("输入要插入的值，为空就输入null");
        System.out.println("输入名字");
        String a = scan.next();
        System.out.println("输入邮箱");
        String a1 = scan.next();
        System.out.println("输入生日");
        String a2 = scan.next();
        try {
            insert(a,a1,a2);
            System.out.println("success");
        } catch (Exception e) {
            System.out.println("false，请输入正确的数据");
        }
    }
    public static void del(Scanner scanner){
        System.out.println("请输入要删除的id");
        int id = scanner.nextInt();
        try {
            int s = delete(id);
            if(s == 0){
                System.out.println("你输入的id不存在");
            }
        } catch (Exception e) {
        }
    }
    //根据id删除
    private static int delete(int id)throws Exception{
        QueryRunner runner = new QueryRunner();
        Connection conn = druid();
        String sql = "delete from customers where id = ?" ;
        int count = runner.update(conn,sql,id);
        System.out.println("删除了" + count + "条记录");
        close(conn,null);
        return count;
    }
    public static void select()throws Exception{
        selectList();
    }
    //查询全部
    private static void selectList()throws Exception{
        QueryRunner runner = new QueryRunner();
        Connection conn = druid();
        String sql = "select id,name,email,birth from customers ";
        BeanListHandler<Customers> handler = new BeanListHandler<>(Customers.class);
        List<Customers> list = runner.query(conn,sql,handler);
        list.forEach(System.out::println);
        close(conn,null);
    }
    private static void update(int id,Object...args) throws Exception{
        QueryRunner qr = new QueryRunner();
        Connection conn = druid();
        String sql = "update customers set name=?, email=?, birth=?  where id = ?";
        int count = qr.update(conn,sql,args[0],args[1],args[2],id);
        System.out.println("添加了" + count + "条记录");
        close(conn, null);
    }
    public static void update(Scanner scan){
        System.out.println("输入要修改的成员的id");
        int id = scan.nextInt();
        System.out.println("输入要修改的值，为空就输入null");
        System.out.println("输入名字");
        String a = scan.next();
        System.out.println("输入邮箱");
        String a1 = scan.next();
        System.out.println("输入生日");
        String a2 = scan.next();
        try {
            update(id,a,a1,a2);
            System.out.println("success");
        } catch (Exception e) {
            System.out.println("false，请输入正确的数据");
        }
    }
}
